package com.sj.parksdk.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;

import com.sj.parksdk.ParkSdk;

import java.util.List;

public class NavicatUtils {

    public static boolean checkMapInstall() {
        boolean isInstallGMap = false;
        List<PackageInfo> packs = ParkSdk.getContext().getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            if (p.versionName == null) {
                continue;
            }
            if ("com.google.android.apps.maps".equals(p.packageName)
                    || ("com.baidu.BaiduMap").equals(p.packageName)
                    || ("com.autonavi.minimap").equals(p.packageName)) {
                isInstallGMap = true;
                break;
            }
        }
        return isInstallGMap;
    }

    public static void navigat(Double orgLon, Double orgLat, String address, Context context) {
        // 跳转导航
        if (!checkMapInstall()) {
            XToastUtils.error("未检测到地图应用，请先下载安装地图应用");
            return;
        }
        if (orgLon != null && orgLat != null) {
            String uristr;
            uristr = "geo:" + orgLat + "," + orgLon + "?q=" + address;
            Uri mUri = Uri.parse(uristr);
            Intent mIntent = new Intent(Intent.ACTION_VIEW, mUri);
            context.startActivity(mIntent);
        } else {
            XToastUtils.error("经纬度异常");
        }
    }
}
